﻿ var geocoder;
 var map;
 var latlocale, lnglocale;
 var DefaultLat = 23.492;
 var DefaultLng = 111.315;
 var  boundsOverlay =null;
 
 var marker;
 
 window.onload=function(){
	// $("mapLoading").style.display='none';
	$("map_canvas").style.display='block';
	initialize();
}
 
function showSearch(){
	$("searchId").style.display='block';
}
function hideSearch(){
	if(marker!=null){
		marker.setMap(null);
	}
	$("searchId").style.display='none';
}
function initialize() {// 初始化
    geocoder = new google.maps.Geocoder();
    directionsDisplay = new google.maps.DirectionsRenderer();
    var latlng = new google.maps.LatLng(DefaultLat, DefaultLng);
    var myOptions = {
      zoom: 18,
      center: latlng,
		navigationControl : true,
		// 比例尺控件
		scaleControl : true,
		mapTypeControl : true,
		mapTypeControlOptions : {
			style : google.maps.MapTypeControlStyle.DROPDOWN_MENU
		},
		zoomControl : true,
		zoomControlOptions : {
			style : google.maps.ZoomControlStyle.ANDROID
		},
		// 街道视图控件
		streetViewControl : true,
		mapTypeId : google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map($("map_canvas"), myOptions);
    //Marker();
   var input = document.getElementById('address');
   new google.maps.places.Autocomplete(input);// 自动匹配地名

      
    
    var latLngControl = new LatLngControl(map);
        
        // Register event listeners
// google.maps.event.addListener(map, 'mouseover', function(mEvent) {
// latLngControl.set('visible', true);
// });
// google.maps.event.addListener(map, 'mouseout', function(mEvent) {
// latLngControl.set('visible', false);
// });
// google.maps.event.addListener(map, 'mousemove', function(mEvent) {
// latLngControl.updatePosition(mEvent.latLng);
 // });
    
 // 按下enter键执行
 enterPress();
}
function codeAddress(address) {// 解析地址
	if(marker!=null){
		marker.setMap(null);
	}
  var address = address || $("address").value;
  if (/\s*^\-?\d+(\.\d+)?\s*\,\s*\-?\d+(\.\d+)?\s*$/.test(address)) {// 如果是经纬度
    var latlng = parseLatLng(address);
    if (latlng == null) {
      $("address").value = "";
    } else {

    }
  } else {// 解析地理位置
    geocoder.geocode({ 'address': address},geo);
  }
}


function parseLatLng(value) {// 格式化经纬度
	value.replace('/\s//g');
	var coords = value.split(',');
	var lat = parseFloat(coords[0]);
	var lng = parseFloat(coords[1]);
	if (isNaN(lat) || isNaN(lng)) {
	  return null;
	} else {
	  return new google.maps.LatLng(lat, lng);
	}
}
function geo(results, status) {// 进行地理解析
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      map.setZoom(10);
      marker = new google.maps.Marker({
          map: map, 
          title: '当前经纬度：'+results[0].geometry.location,
          position: results[0].geometry.location,
          draggable: false,// true
      });
      updateMarkerPosition(results[0].geometry.location);
      geocodePosition(results[0].geometry.location);

    } else {
      alert("Geocode was not successful for the following reason: " + status);
    }     
}

function enterPress(){// 按下enter键
   $("address").onkeyup = function(e) {
  if (!e) var e = window.event;
  if (e.keyCode != 13) return;
  $("go").click();
 }
}

function geocodePosition(pos) {// 改变经纬度时获取信息
  geocoder.geocode({
    latLng: pos
  }, function(responses) {
    if (responses && responses.length > 0) {
      // updateMarkerAddress(responses[0].formatted_address);
    } else {
      // updateMarkerAddress('地址不能正确解析');
    }
  });
}

function $(id){
  return document.getElementById(id);
}
function LatLngControl(map) {
        /**
		 * Offset the control container from the mouse by this amount.
		 */
        this.ANCHOR_OFFSET_ = new google.maps.Point(8, 8);
        
        /**
		 * Pointer to the HTML container.
		 */
        this.node_ = this.createHtmlNode_();
        
        // Add control to the map. Position is irrelevant.
        map.controls[google.maps.ControlPosition.TOP].push(this.node_);
        
        // Bind this OverlayView to the map so we can access MapCanvasProjection
        // to convert LatLng to Point coordinates.
        this.setMap(map);
        
        // Register an MVC property to indicate whether this custom control
        // is visible or hidden. Initially hide control until mouse is over map.
        this.set('visible', false);
      }
      
      // Extend OverlayView so we can access MapCanvasProjection.
      LatLngControl.prototype = new google.maps.OverlayView();
      LatLngControl.prototype.draw = function() {};
      
      /**
		 * @private Helper function creates the HTML node which is the control
		 *          container.
		 * @return {HTMLDivElement}
		 */
      LatLngControl.prototype.createHtmlNode_ = function() {
        var divNode = document.createElement('div');
        divNode.id = 'latlng-control';
        divNode.index = 9999;
        return divNode;
      };
      
      /**
		 * MVC property's state change handler function to show/hide the control
		 * container.
		 */
      LatLngControl.prototype.visible_changed = function() {
        this.node_.style.display = this.get('visible') ? '' : 'none';
      };